/*
 * Copyright 2012 LMAX Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmax.disruptor;

import static java.util.Arrays.copyOf;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * Provides static methods for managing a {@link SequenceGroup} object.
 */
class SequenceGroups
{
    //    // 线程安全的 sequencesToAdd 加到门禁集合
    static <T> void addSequences(
        final T holder, // 生产者引用
        final AtomicReferenceFieldUpdater<T, Sequence[]> updater, // 存放了生产者引用的  门禁集合（生产者序号肯定不能超过这个门禁集合的最小值）
        final Cursored cursor,
        final Sequence... sequencesToAdd)
    {
        long cursorSequence;
        Sequence[] updatedSequences;
        Sequence[] currentSequences;
        // 这里用了 cas 自旋
        do
        {
            currentSequences = updater.get(holder);

            //此处为 copyOf(...) 方法为 java.util.Arrays.copyOf(...) 方法，用于将 currentSequences 复制一份
            //使用了 写时复制 的思想  ：读写分离   真正写的时候用的自旋
            updatedSequences = copyOf(currentSequences, currentSequences.length + sequencesToAdd.length);
           //此处的 cursor 即为生产者 ProducerSequencer.getCursor() 的 游标
            // 通过 游标，获取 当前的 生产 序号
            cursorSequence = cursor.getCursor(); //这个是当前生产者生产的序号

            int index = currentSequences.length;
            for (Sequence sequence : sequencesToAdd)
            {

                sequence.set(cursorSequence);
                updatedSequences[index++] = sequence;
            }
        }
        while (!updater.compareAndSet(holder, currentSequences, updatedSequences));
       //此处的 while 会死循环 CAS 操做直到更新成功

        // 这里为什么又用了一次呢？
        cursorSequence = cursor.getCursor();
        for (Sequence sequence : sequencesToAdd)
        {
            sequence.set(cursorSequence);
        }
    }
    // 线程安全的 把sequence从门禁集合中移除
    static <T> boolean removeSequence(
        final T holder,
        final AtomicReferenceFieldUpdater<T, Sequence[]> sequenceUpdater,
        final Sequence sequence)
    {
        int numToRemove;
        Sequence[] oldSequences;
        Sequence[] newSequences;

        do
        {
            // 生产者门禁集合
            oldSequences = sequenceUpdater.get(holder);
            // countMatching找到 sequence序号的个数
            numToRemove = countMatching(oldSequences, sequence);

            if (0 == numToRemove)
            {
                break;
            }

            final int oldSize = oldSequences.length;
            newSequences = new Sequence[oldSize - numToRemove];

            for (int i = 0, pos = 0; i < oldSize; i++)
            {
                final Sequence testSequence = oldSequences[i];
                if (sequence != testSequence)
                {
                    newSequences[pos++] = testSequence;
                }
            }
        }
        while (!sequenceUpdater.compareAndSet(holder, oldSequences, newSequences));

        return numToRemove != 0;
    }

    private static <T> int countMatching(T[] values, final T toMatch)
    {
        int numToRemove = 0;
        for (T value : values)
        {
            if (value == toMatch) // Specifically uses identity
            {
                numToRemove++;
            }
        }
        return numToRemove;
    }
}
